package com.bhs.sansonglogistics.ui.wallet;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import android.content.Intent;
import android.os.Bundle;
import android.util.ArrayMap;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import com.bhs.sansonglogistics.R;
import com.bhs.sansonglogistics.base.BaseActivity;
import com.bhs.sansonglogistics.bean.BankCardData;
import com.bhs.sansonglogistics.bean.WithdrawalData;
import com.bhs.sansonglogistics.bean.WithdrawalRecordBean;
import com.bhs.sansonglogistics.bean.WithdrawalRecordData;
import com.bhs.sansonglogistics.net.NetCallBack;
import com.bhs.sansonglogistics.utils.DateUtils;
import com.bhs.sansonglogistics.utils.MyUtils;
import com.bhs.sansonglogistics.utils.ToastUtil;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.google.gson.Gson;

import java.util.Map;

import okhttp3.ResponseBody;
import retrofit2.Response;

/**
 * 提现记录
 */
public class WithdrawalRecordActivity extends BaseActivity implements NetCallBack {

    private SwipeRefreshLayout mRefreshLayout;
    private RecyclerView mRvList;
    private BaseQuickAdapter<WithdrawalRecordBean, BaseViewHolder> mAdapter;
    private int page = 1;

    @Override
    public int initLayout() {
        return R.layout.activity_withdrawal_record;
    }

    @Override
    public void initView() {
        TextView mTvTitle = findViewById(R.id.tv_title);
        mTvTitle.setText("提现记录");
        mRefreshLayout = findViewById(R.id.refreshLayout);
        mRvList = findViewById(R.id.rv_list);
    }

    @Override
    public void initData() {
        mAdapter = new BaseQuickAdapter<WithdrawalRecordBean, BaseViewHolder>(R.layout.item_withdrawal_record) {
            @Override
            protected void convert(@NonNull BaseViewHolder helper, WithdrawalRecordBean item) {
               helper.setText(R.id.tv_money,String.format("￥%s",item.getMoney()));
                helper.setText(R.id.tv_time, DateUtils.getTime2(item.getCreate_time()));
                helper.setText(R.id.tv_status,item.getStatus_msg());
                String bank_number = item.getBank_number();
                bank_number = bank_number.substring(bank_number.length() - 4);
                helper.setText(R.id.tv_type_withdrawal,String.format("提现到%s", MyUtils.getPayType(item.getBank_id())));
                helper.setText(R.id.tv_bank_card,String.format("%s(%s)",item.getBank_name(),bank_number));
                if (item.getBank_id() > 2) {
                    helper.setText(R.id.tv_bank_card,String.format("%s(%s)",item.getBank_name(),bank_number));
                } else {
                    helper.setText(R.id.tv_bank_card,item.getBank_name());
                }
            }
        };
        mRvList.setAdapter(mAdapter);
        mRvList.setLayoutManager(new LinearLayoutManager(mActivity));
        mAdapter.setEmptyView(LayoutInflater.from(mActivity).inflate(R.layout.layout_empty, null));
        loadData();
        mRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                page = 1;
                loadData();
            }
        });

        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                page++;
                loadData();
            }
        }, mRvList);
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                startActivity(new Intent(mActivity,WithdrawalProcessActivity.class)
                        .putExtra("withdraw_id",mAdapter.getData().get(position).getWithdraw_id()));
            }
        });
    }

    private void loadData() {
        Map<String,String> map=new ArrayMap<>();
        map.put("page",String.valueOf(page));
        networkRequest(netApi.get_withdraw_log(map),this);
    }

    @Override
    public void onResponse(int requestCode, String json, Response<ResponseBody> response) throws Exception {
        WithdrawalRecordData bean = new Gson().fromJson(json, WithdrawalRecordData.class);
        mRefreshLayout.setRefreshing(false);
        if(bean.isStatus()){
            if (page == 1) {
                mAdapter.setNewData(bean.getData().getList());
            } else {
                mAdapter.loadMoreComplete();
                mAdapter.addData(bean.getData().getList());
            }
            if (mAdapter.getData().size() >= bean.getData().getTotal_count()) {
                mAdapter.loadMoreEnd();
            }
        }else {
            ToastUtil.show(bean.getMsg());
        }

    }

    @Override
    public void onFailure(int requestCode) {
        mRefreshLayout.setRefreshing(false);
        mAdapter.loadMoreComplete();
    }
}